package com.androidx.p013lv.base.utils;

import android.content.Context;
import android.os.Environment;
import java.io.File;
import java.math.BigDecimal;

/* renamed from: com.androidx.lv.base.utils.DataCleanManager */
public class DataCleanManager {
    public static String getTotalCacheSize(Context context) {
        long j = 0;
        try {
            long folderSize = getFolderSize(context.getCacheDir());
            if (Environment.getExternalStorageState().equals("mounted")) {
                folderSize += getFolderSize(context.getExternalCacheDir());
            }
            j = folderSize + getFolderSize(context.getExternalFilesDir(null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getFormatSize((double) j);
    }

    public static void clearAllCache(Context context) {
        deleteDir(context.getCacheDir());
        if (Environment.getExternalStorageState().equals("mounted")) {
            deleteDir(context.getExternalCacheDir());
        }
        deleteDir(context.getExternalFilesDir(null));
    }

    private static boolean deleteDir(File file) {
        String[] list;
        if (file != null && file.isDirectory()) {
            for (String str : file.list()) {
                if (!deleteDir(new File(file, str))) {
                    return false;
                }
            }
        }
        return file.delete();
    }

    public static long getFolderSize(File file) throws Exception {
        long j;
        long j2 = 0;
        try {
            File[] listFiles = file.listFiles();
            for (int i = 0; i < listFiles.length; i++) {
                if (listFiles[i].isDirectory()) {
                    j = getFolderSize(listFiles[i]);
                } else {
                    j = listFiles[i].length();
                }
                j2 += j;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return j2;
    }

    public static String getFormatSize(double d) {
        double d2 = d / 1024.0d;
        if (d2 < 1.0d) {
            return "0M";
        }
        double d3 = d2 / 1024.0d;
        if (d3 < 1.0d) {
            BigDecimal bigDecimal = new BigDecimal(Double.toString(d2));
            return bigDecimal.setScale(2, 4).toPlainString() + "K";
        }
        double d4 = d3 / 1024.0d;
        if (d4 < 1.0d) {
            BigDecimal bigDecimal2 = new BigDecimal(Double.toString(d3));
            return bigDecimal2.setScale(2, 4).toPlainString() + "M";
        }
        double d5 = d4 / 1024.0d;
        if (d5 < 1.0d) {
            BigDecimal bigDecimal3 = new BigDecimal(Double.toString(d4));
            return bigDecimal3.setScale(2, 4).toPlainString() + "GB";
        }
        BigDecimal bigDecimal4 = new BigDecimal(d5);
        return bigDecimal4.setScale(2, 4).toPlainString() + "TB";
    }
}
